package com.shen.eduorder.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shen.commonutils.JwtUtils;
import com.shen.commonutils.Result;
import com.shen.eduorder.entity.TOrder;
import com.shen.eduorder.service.TOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author shenkaituo
 * @since 2020-07-08
 */
@RestController
@RequestMapping("/eduorder/order")
@CrossOrigin
public class TOrderController {

    @Autowired
    private TOrderService orderService;

    @GetMapping("createOrder/{courseId}")
    public Result saveOrder(@PathVariable String courseId, HttpServletRequest request){
        // id 存放在 request里面
        System.out.println(" order Controller------");
        System.out.println(" this is courseID----"+courseId);
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        // 创建订单 返回订单号
       String orderNo= orderService.createOrders(courseId,userId);
        return Result.ok().data("orderId",orderNo);
    }

    @GetMapping("getOrderInfo/{orderId}")
    public  Result getOrderInfo(@PathVariable String orderId){
        QueryWrapper<TOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("order_no",orderId);
        TOrder order = orderService.getOne(queryWrapper);
        return  Result.ok().data("item",order);
    }

    @GetMapping("getStatus/{courseId}/{userId}")
    public  boolean getStatus(@PathVariable("courseId") String courseId,@PathVariable("userId") String userId){
        QueryWrapper<TOrder> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        queryWrapper.eq("member_id",userId);
        queryWrapper.eq("status",1);//支付状态
        int count = orderService.count(queryWrapper);
        if(count>0){return  true;}
        else return false;
    }
}

